package com.wc.alorithm_luogu.P4913;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author congge
 * @Date 2023/5/22 9:56
 * @description https://www.luogu.com.cn/problem/P4913
 * 【深基16.例3】二叉树深度
 */
public class Main {
    public static void main(String[] args) {
        Input sc = new Input();
        int n = sc.nextInt();


        HashMap<Integer, TreeNode> map = new HashMap<>();
        map.put(0, null);
        for (int i = 1; i <= n; i++) {
            TreeNode node = new TreeNode();
            node.val = i;
            node.leftVal = sc.nextInt();
            node.rightVal = sc.nextInt();
            map.put(i, node);
        }
//        map.get(1).height = 1;

//        int ans = 1;
//        if (n != 9) {
            for (Map.Entry<Integer, TreeNode> entry : map.entrySet()) {
                if (entry.getKey() == 0) {
                    continue;
                }
                TreeNode node = entry.getValue();
                node.leftNode = map.get(node.leftVal);
//                if (node.leftVal != 0) {
//                    node.leftNode.height = node.height + 1;
//                    ans = Math.max(node.leftNode.height, ans);
//                }
                node.rightNode = map.get(node.rightVal);
//                if (node.rightVal != 0) {
//                    node.rightNode.height = node.height + 1;
//                    ans = Math.max(node.rightNode.height, ans);
//                }
            }
//        } else {
//            getHigh(map.get(1), map);
//            for (Map.Entry<Integer, TreeNode> entry : map.entrySet()) {
//                if (entry.getKey() != 0) {
//                    ans = Math.max(ans, entry.getValue().height);
//                }
//            }
//        }

        System.out.println(getHigh(map.get(1)));
    }


    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }

    static int getHigh(TreeNode root) {
        if (root.leftVal == 0 && root.rightVal == 0) {
            return 1;
        }
        if (root.leftVal == 0) {
            return 1 + getHigh(root.rightNode);
        }
        if (root.rightVal == 0) {
            return 1 + getHigh(root.leftNode);
        }
        return 1 + Math.max(getHigh(root.leftNode), getHigh(root.rightNode));
    }

    static void getHigh(TreeNode root, HashMap<Integer, TreeNode> map) {
        if (root == null) {
            return;
        }
        TreeNode left = map.get(root.leftVal);
        TreeNode right = map.get(root.rightVal);
        if (left != null)
            left.height = root.height + 1;
        if (right != null)
            right.height = root.height + 1;
        getHigh(left, map);
        getHigh(right, map);
    }
}

class TreeNode {
    int val;
    int leftVal;
    int rightVal;
    int height;

    TreeNode leftNode;
    TreeNode rightNode;
}
